import json

from com.maycur.gateway.common.base_entity import ApiTaskSet, SequentialApiTaskSet
from locust import task, tag

from com.maycur.utils import TimeUtils


class DataPremission(SequentialApiTaskSet):
    group_code: str
    min_wait = 1
    max_wait = 2

    @tag('tag0')
    @task
    def detail_permission_group(self):
        detail_permission_group_url = f'{self.get_app_host()}/api/web/authz/v2/data/permission/group/search'
        request_body = {
            "keyword": ""
        }
        json_body = json.dumps(request_body)
        headers = self.get_headers_with_content_type()
        with self.client.post(detail_permission_group_url, data=json_body, headers=headers, catch_response=True) as res:
            result_dto = self.parse_response(res)
            if result_dto is not None:
                print(result_dto.code)

    @task
    def add_premission_group(self):
        detail_permission_group_url = f'{self.get_app_host()}/api/web/authz/v2/data/permission/group'
        timestamp = TimeUtils.current_timestamp()
        headers = self.get_headers_with_content_type()
        request_body = {"groupName":f'{timestamp}',"basicDataPermOptions":[{"permName":"组","permCode":"MANAGE_USER_GROUP","departmentScopeMode":"ALL"},{"permName":"公司抬头","permCode":"MANAGE_LEGAL_ENTITY","departmentScopeMode":"ALL"},{"permName":"组织架构&员工管理","permCode":"ORGANIZATION_MANAGE","departmentScopeMode":"ALL"},{"permName":"多维费控","permCode":"MANAGE_COST_CTRL","departmentScopeMode":"ALL"},{"permName":"往来单位","permCode":"MANAGE_TRADING_PARTNER","departmentScopeMode":"ALL"},{"permName":"费用兑换积分","permCode":"INTEGRAL_EXPENSE","departmentScopeMode":"ALL"},{"permName":"表单与流程管理","permCode":"MANAGE_RECEIPTS","departmentScopeMode":"ALL"},{"permName":"自定义档案","permCode":"MAINTAIN_OPTION_GROUP","departmentScopeMode":"ALL"}],"dataPermOptions":[{"permCode":"EXPORT_VOUCHER","permName":"凭证生成","departmentScopeMode":"ALL","departmentScopes":[],"legalEntityScopeMode":"ALL","legalEntityScopes":[],"formSubTypeScopeMode":"ALL","formSubTypeScopes":[],"budgetOrgScopeMode":"NO_LIMIT","budgetOrgScopes":[],"optionScopeMode":"NO_LIMIT","optionScopes":[]},{"permCode":"VOUCHER_QUERY","permName":"凭证查询","departmentScopeMode":"NO_LIMIT","departmentScopes":[],"legalEntityScopeMode":"ALL","legalEntityScopes":[],"formSubTypeScopeMode":"NO_LIMIT","formSubTypeScopes":[],"budgetOrgScopeMode":"NO_LIMIT","budgetOrgScopes":[],"optionScopeMode":"NO_LIMIT","optionScopes":[]},{"permCode":"MANAGE_LOAN","permName":"借款管理","departmentScopeMode":"NO_LIMIT","departmentScopes":[],"legalEntityScopeMode":"ALL","legalEntityScopes":[],"formSubTypeScopeMode":"NO_LIMIT","formSubTypeScopes":[],"budgetOrgScopeMode":"NO_LIMIT","budgetOrgScopes":[],"optionScopeMode":"NO_LIMIT","optionScopes":[]},{"permCode":"QUERY_FORM","permName":"查询单据","departmentScopeMode":"ALL","departmentScopes":[],"legalEntityScopeMode":"ALL","legalEntityScopes":[],"formSubTypeScopeMode":"ALL","formSubTypeScopes":[],"budgetOrgScopeMode":"NO_LIMIT","budgetOrgScopes":[],"optionScopeMode":"NO_LIMIT","optionScopes":[]},{"permCode":"PAID_HISTORY","permName":"支付台账","departmentScopeMode":"ALL","departmentScopes":[],"legalEntityScopeMode":"ALL","legalEntityScopes":[],"formSubTypeScopeMode":"ALL","formSubTypeScopes":[],"budgetOrgScopeMode":"NO_LIMIT","budgetOrgScopes":[],"optionScopeMode":"NO_LIMIT","optionScopes":[]},{"permCode":"TRADING_PARTNER_ACCOUNT_BOOK","permName":"往来单位台账","departmentScopeMode":"ALL","departmentScopes":[],"legalEntityScopeMode":"ALL","legalEntityScopes":[],"formSubTypeScopeMode":"ALL","formSubTypeScopes":[],"budgetOrgScopeMode":"NO_LIMIT","budgetOrgScopes":[],"optionScopeMode":"NO_LIMIT","optionScopes":[]},{"permCode":"INVOICE_MANAGE","permName":"发票台账","departmentScopeMode":"NO_LIMIT","departmentScopes":[],"legalEntityScopeMode":"ALL","legalEntityScopes":[],"formSubTypeScopeMode":"ALL","formSubTypeScopes":[],"budgetOrgScopeMode":"NO_LIMIT","budgetOrgScopes":[],"optionScopeMode":"NO_LIMIT","optionScopes":[]},{"departmentScopeMode":"ALL","departmentScopes":[],"formSubTypeScopeMode":"NO_LIMIT","formSubTypeScopes":[],"legalEntityScopeMode":"ALL","legalEntityScopes":[],"permCode":"ACCOUNT_BOOK_CONTRACT","permName":"合同台账","budgetOrgScopeMode":"NO_LIMIT","budgetOrgs":[],"optionScopeMode":"NO_LIMIT","optionScopes":[]},{"permCode":"BUDGET_LEDGER","permName":"预算台账","departmentScopeMode":"NO_LIMIT","departmentScopes":[],"legalEntityScopeMode":"NO_LIMIT","legalEntityScopes":[],"formSubTypeScopeMode":"NO_LIMIT","formSubTypeScopes":[],"budgetOrgScopeMode":"ALL","budgetOrgScopes":[],"optionScopeMode":"NO_LIMIT","optionScopes":[]},{"permCode":"MANAGE_FORM_PROCESS","permName":"审批流程处理","departmentScopeMode":"NO_LIMIT","departmentScopes":[],"legalEntityScopeMode":"ALL","legalEntityScopes":[],"formSubTypeScopeMode":"NO_LIMIT","formSubTypeScopes":[],"budgetOrgScopeMode":"NO_LIMIT","budgetOrgScopes":[],"optionScopeMode":"NO_LIMIT","optionScopes":[]},{"permCode":"MANAGE_SIGN_PROCESS","permName":"签收流程处理","departmentScopeMode":"NO_LIMIT","departmentScopes":[],"legalEntityScopeMode":"ALL","legalEntityScopes":[],"formSubTypeScopeMode":"NO_LIMIT","formSubTypeScopes":[],"budgetOrgScopeMode":"NO_LIMIT","budgetOrgScopes":[],"optionScopeMode":"NO_LIMIT","optionScopes":[]}],"iscache":True}
        json_body = json.dumps(request_body)
        with self.client.post(detail_permission_group_url, data=json_body, headers=headers, catch_response=True) as res:
            result_dto = self.parse_response(res)
            if result_dto is None:
                self.group_code = None
                return
            self.group_code = result_dto.data['groupCode']
            print(self.group_code)

    @task
    def delete_premission_group(self):
        if self.group_code is None:
            return
        permission_group_url = f'{self.get_app_host()}/api/web/authz/v2/data/permission/group/{self.group_code}'
        with self.client.delete(permission_group_url, headers=self.get_headers_with_content_type(), catch_response=True) as res:
            self.parse_response(res)

